/*
 * Sun.fx
 *
 * Created on 06.11.2009, 20:04:48
 */

package javafx179labs.nez.lab4;

import javafx.scene.CustomNode;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.animation.transition.SequentialTransition;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.Group;
import java.lang.Math;
import javafx.scene.shape.Circle;
import javafx.scene.Node;

/**
 * @author civil
 */

public class Sun extends CustomNode {
    public var beginR: Float;
    public  var endR: Float;
    public  var x: Float;
    public var y: Float;

    var rotate1: Float = 45;
    var rotate2: Float = 0;
    var tr1 = SequentialTransition {
    repeatCount: SequentialTransition.INDEFINITE
        time: 2s
        content: [
                Timeline {
                    repeatCount: Timeline.INDEFINITE
                    keyFrames: [
                            KeyFrame {
                                time: 2s
                                values: [
                                      rotate1 => 405,
                                      rotate2 => 360
                                ]
                            }
                    ]
                }
                Timeline {
                    repeatCount: Timeline.INDEFINITE
                    keyFrames: [
                            KeyFrame {
                                time: 0s
                                action: function () {
                                    rotate1 = 45;
                                }
                            }
                    ]
                }
        ]
    }
    var rotate3: Float = 405;
    var rotate4: Float = 360;
    var tr2 = SequentialTransition {
        repeatCount: SequentialTransition.INDEFINITE
        time: 2s
        content: [
                Timeline {
                    repeatCount: Timeline.INDEFINITE
                    keyFrames: [
                            KeyFrame {
                                time: 2s
                                values: [
                                      rotate3 => 45,
                                      rotate4 => 0
                                ]
                            }
                    ]
                }
                Timeline {
                    repeatCount: Timeline.INDEFINITE
                    keyFrames: [
                            KeyFrame {
                                time: 0s
                                action: function () {
                                    rotate3 = 405;
                                    rotate4 = 360;
                                }
                            }
                    ]
                }
        ]
    }

    public function play(): Void {
        tr1.play();
        tr2.play();
    }

    override public function create(): Node {
        var seq: Float[] = [beginR..endR step 30];
        var curR = beginR;
        while (curR <= endR) {
            insert curR as Float into seq;
            curR = Math.sqrt(curR * curR + curR * curR);
        }
        var flag: Boolean = true;


        Group {
            content: [for(r in reverse seq) {
                flag = not flag;
                Group {
                 content: [
                    if (flag) {
                        [Rectangle {
                            x: x - r / 2
                            y: y - r / 2
                            height: r
                            width: r
                            fill: Color.ORANGE
                            rotate: bind rotate1
                        }
                        Rectangle {
                            x: x - r / 2
                            y: y - r / 2
                            height: r
                            width: r
                            fill: Color.ORANGE
                            rotate: bind rotate2
                        }]
                    } else {
                        [Rectangle {
                            x: x - r / 2
                            y: y - r / 2
                            height: r
                            width: r
                            fill: Color.ORANGE
                            rotate: bind rotate3
                        }
                        Rectangle {
                            x: x - r / 2
                            y: y - r / 2
                            height: r
                            width: r
                            fill: Color.ORANGE
                            rotate: bind rotate4
                        }]
                    }
                    Circle {
                        centerX: x
                        centerY: y
                        radius: r * 0.6
                        fill: Color.YELLOW
                        stroke: Color.ORANGE
                    }]}
            }]
        }
    }

}
